package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.UserDeclaration;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeclarationMapper;
import com.ruoyi.system.domain.Declaration;
import com.ruoyi.system.service.IDeclarationService;
import com.ruoyi.common.core.text.Convert;

/**
 * 报单数据管理Service业务层处理
 * 
 * @author 海田精品馆
 * @date 2022-07-27
 */
@Service
public class DeclarationServiceImpl implements IDeclarationService 
{
    @Autowired
    private DeclarationMapper declarationMapper;

    /**
     * 查询报单数据管理
     * 
     * @param decId 报单数据管理主键
     * @return 报单数据管理
     */
    @Override
    public Declaration selectDeclarationByDecId(Long decId)
    {
        return declarationMapper.selectDeclarationByDecId(decId);
    }

    /**
     * 查询报单数据管理列表
     * 
     * @param declaration 报单数据管理
     * @return 报单数据管理
     */
    @Override
    public List<Declaration> selectDeclarationList(Declaration declaration)
    {
        return declarationMapper.selectDeclarationList(declaration);
    }

    /**
     * 新增报单数据管理
     * 
     * @param declaration 报单数据管理
     * @return 结果
     */
    @Override
    public int insertDeclaration(Declaration declaration) throws Exception {
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setCreateBy(sysUser.getUserName());
        declaration.setCreateTime(DateUtils.getNowDate());
        UserDeclaration userDeclaration1 = new UserDeclaration();
        userDeclaration1.setDecOrderNo(declaration.getDecOrderNo());
        List<Declaration> userDeclarations = declarationMapper.selectDeclarationList(declaration);
        if (null != userDeclarations && !userDeclarations.isEmpty()) {
            throw new Exception("已存在单号" + declaration.getDecOrderNo());
        }
        return declarationMapper.insertDeclaration(declaration);
    }

    /**
     * 修改报单数据管理
     * 
     * @param declaration 报单数据管理
     * @return 结果
     */
    @Override
    public int updateDeclaration(Declaration declaration)
    {
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setUpdateBy(sysUser.getLoginName());
        declaration.setUpdateTime(DateUtils.getNowDate());
        return declarationMapper.updateDeclaration(declaration);
    }

    /**
     * 批量删除报单数据管理
     * 
     * @param decIds 需要删除的报单数据管理主键
     * @return 结果
     */
    @Override
    public int deleteDeclarationByDecIds(String decIds)
    {
        return declarationMapper.deleteDeclarationByDecIds(Convert.toStrArray(decIds));
    }

    /**
     * 删除报单数据管理信息
     * 
     * @param decId 报单数据管理主键
     * @return 结果
     */
    @Override
    public int deleteDeclarationByDecId(Long decId)
    {
        return declarationMapper.deleteDeclarationByDecId(decId);
    }
}
